<?php

namespace App\Http\Controllers;

use App\Models\BlogModel;
use Illuminate\Http\Request;
use App\Models\CommentModel;
use Illuminate\Support\Facades\Auth;

class commentController extends Controller
{
    //评论
    public function comment(Request $request){
        if($request->isMethod('POST')) {
            $arr = [
                'content'=>$request->comment,
                'user_id'=>$request->user_id,
                'blog_id'=>$request->blog_id,
            ];
            if(CommentModel::insert($arr)){
//                return redirect('/detail');
                echo '<script>self.location=document.referrer;</script>';
            }else{
                echo '<script>alert(\'评论失败\');self.location=document.referrer;</script>';
            }
        }
    }

//    删除评论
    public  function deletcomment(Request $request){
        if (Auth::guard('admin')->id()==2){
            CommentModel:: where('comment_id',$request->id)->delete();
            echo '<script>self.location=document.referrer;</script>';
        }else{
            $commentdataid =  CommentModel::firstWhere('comment_id','=',$request->id)['user_id'];
            if(Auth::guard('admin')->user()['user_id']==$commentdataid){
                CommentModel:: where('comment_id',$request->id)->delete();
                echo '<script>alert(\'删除成功\');self.location=document.referrer;</script>';
            }else{
                echo '<script>alert(\'您不是管理员，无法删除他人的评论\');self.location=document.referrer;</script>';
            }

        }
    }


}
